
function add_div(x, y, w, h, hide, css, divid, callback) {
    // 确保参数有默认值
    x = x || 100;
    y = y || 100;
    w = w || 200;
    h = h || 200;
    hide = hide || false;
    css = css || '';
    divid = divid || false;
    callback = callback || null;
    
    // 生成唯一ID
    const id = 'div_' + Date.now() + '_' + Math.floor(Math.random() * 10000);
    
    // 创建div元素
    const div = document.createElement('div');
    div.id = id;
    
    // 设置样式
    div.style.position = 'absolute';
    div.style.left = x + 'px';
    div.style.top = y + 'px';
    div.style.width = w + 'px';
    div.style.height = h + 'px';
    div.style.overflow = 'hidden';
    
    // 附加样式
    if (css) div.style.cssText += css;
    if (hide) div.style.display = 'none';
    
    // 添加到页面或指定父div
    const parent = divid ? document.getElementById(divid) : document.body;
    parent.appendChild(div);
    
    // 初始化divs存储空间
    if (!elementStore.divs) {
        elementStore.divs = {};
    }
    
    // 存储引用
    elementStore.divs[id] = div;
    
    // 添加事件处理
    div.onmousemove = function(e) { handleEvent('div', id, 0, '', '', e.clientX, e.clientY, null, callback, divid); };
    div.onclick = function(e) { handleEvent('div', id, 1, '', '', e.clientX, e.clientY, null, callback, divid); };
    div.oncontextmenu = function(e) { e.preventDefault(); handleEvent('div', id, 2, '', '', e.clientX, e.clientY, null, callback, divid); return false; };
    div.onmouseover = function(e) { handleEvent('div', id, 5, '', '', e.clientX, e.clientY, null, callback, divid); };
    div.onmouseout = function(e) { handleEvent('div', id, 6, '', '', e.clientX, e.clientY, null, callback, divid); };
    div.onkeydown = function(e) { handleEvent('div', id, 3, '', '', 0, 0, e.key, callback, divid); };
    div.onkeyup = function(e) { handleEvent('div', id, 4, '', '', 0, 0, e.key, callback, divid); };
    
    return id;
}

window.del_div = function(id) {
    if (elementStore.divs[id]) {
        elementStore.divs[id].remove();
        delete elementStore.divs[id];
    }
}

window.upd_div = function(id, x, y, w, h, hide, css) {
    const div = elementStore.divs[id];
    if (div) {
        if (x !== undefined) div.style.left = x + 'px';
        if (y !== undefined) div.style.top = y + 'px';
        if (w !== undefined) div.style.width = w + 'px';
        if (h !== undefined) div.style.height = h + 'px';
        if (hide !== undefined) div.style.display = hide ? 'none' : 'block';
        if (css !== undefined) div.style.cssText = css;
    }
}